package model.handlers;

import database.Database;
import java.sql.SQLException;
import model.ProductFamily;

/**
 *
 * @author Sonny Sandberg
 */
public abstract class ProductFamilyHandler 
{
    public static ProductFamily[] getProductFamilyArray() throws ClassNotFoundException, SQLException
    {
        ProductFamily[] productFamilyArray = null;
        
        Database db = new Database();
        
        String[][] result = db.getFromDB("bs_product_family", "", "ORDER BY name ASC", "", "id", "name");
        productFamilyArray = new ProductFamily[result.length];
        
        db.closeDB();
        
        for (int i = 0; i < result.length; i++)
        {
            productFamilyArray[i] = new ProductFamily(Integer.parseInt(result[i][0]), result[i][1]);
        }
        
        return productFamilyArray;
    }
    
    public static ProductFamily[] getProductFamilyArrayBySearch(String searchString) throws ClassNotFoundException, SQLException
    {
        ProductFamily[] productFamilyArray = null;
        
        Database db = new Database();
        
        String[][] result = db.getFromDB("bs_product_family", "WHERE name LIKE '%"+searchString+"%'", "ORDER BY name ASC", "", "id", "name");
        productFamilyArray = new ProductFamily[result.length];
        
        db.closeDB();
        
        for (int i = 0; i < result.length; i++)
        {
            productFamilyArray[i] = new ProductFamily(Integer.parseInt(result[i][0]), result[i][1]);
        }
        
        return productFamilyArray;
    }
    
    public static ProductFamily getProductFamilyByID(int id) throws ClassNotFoundException, SQLException{
        
        ProductFamily productFamily = null;
        
        Database db = new Database();        
        String[][] result = db.getFromDB("bs_product_family", "where id = '"+id+"'", "", "", "id", "name");
        db.closeDB();
        
        productFamily = new ProductFamily(Integer.parseInt(result[0][0]), result[0][1]);        
        
        return productFamily;        
    }
}